package com.nami.pchat.infra.middleware.mq.producer;

import com.nami.gate.external.basic.constants.ConverTopicConst;
import com.nami.gate.external.basic.constants.DispatcherTopicConst;
import com.nami.gate.external.basic.constants.StoreTopicConst;
import com.nami.gate.external.basic.constants.UpdCacheTopicConst;
import com.nami.gate.external.basic.event.EventPublisher;
import com.nami.gate.external.basic.event.model.CommonEvent;
import com.nami.transport.proto.codec.MqTransMsg;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;

/**
 * DefaultEventPublisher
 *
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PChatEventPublisher implements EventPublisher {

    private final RocketMQTemplate rocketMQTemplate;

    @Override
    public void publish(CommonEvent event) {
        try {
            switch (event.getEventType()) {
                case SEND_DISPATCHER:
                    MqTransMsg.Logic2DispatchBody trans = (MqTransMsg.Logic2DispatchBody) event.getPayload();
                    String userId = trans.getSender().getUserId();
                    rocketMQTemplate.syncSendOrderly(DispatcherTopicConst.PCHAT_TOPIC, trans.toByteArray(), userId);
                    break;
                case UPD_PRIVATE_CONVERS:
                    MqTransMsg.Logic2DispatchBody convers = (MqTransMsg.Logic2DispatchBody) event.getPayload();
                    String usrId = convers.getSender().getUserId();
                    rocketMQTemplate.syncSendOrderly(ConverTopicConst.PCHAT_UPD_CONVERS_TOPIC, convers.toByteArray(), usrId);
                    break;
                case STORE_PRIVATE_MSG:
                    MqTransMsg.Logic2DispatchBody storeMsg = (MqTransMsg.Logic2DispatchBody) event.getPayload();
//                    Message message = Message.newBuilder()
//                        .setBody(storeMsg.toByteString())
//                        .build();
                    rocketMQTemplate.syncSend(StoreTopicConst.PCHAT_STORE_MSG, storeMsg.toByteArray());
                    break;
                case REFRESH_L1_USER_CACHE:
                    MqTransMsg.Logic2DispatchBody l1User = (MqTransMsg.Logic2DispatchBody) event.getPayload();
                    rocketMQTemplate.syncSend(UpdCacheTopicConst.REFRESH_USER_TOPIC, l1User.toByteArray());
                    break;
                default:
                    log.error("[c2c] 单聊消费者未匹配类型：{}", event.getEventType());
                    break;
            }
        } catch (Exception var2) {
            var2.printStackTrace();
            log.error("[c2c-publisher] 单聊消费者出现问题：", var2);
        }
    }

}
